/**
 * Copyright(c) Hainan eKing Technology co., LTD
 *
 * @date 2013年9月11日
 * 	
 * Original Author: 郑兴(zhengxing)
 *
 * ChangeLog
 *
 */
package com.hnas.sys.service.system.mapper;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

import com.hnas.core.mapper.Mapper;
import com.hnas.core.util.StringUtil;
import com.hnas.sys.orm.User;
import com.hnas.sys.service.system.dto.DTOUser;

/**
 * @author 郑兴(zhengxing)
 *
 */
public class UserMapper extends Mapper<DTOUser, User> {
	private static UserMapper userMapper;
	
	private UserMapper() {
		super(DTOUser.class, User.class);
	}
	
	public static UserMapper getInstance() {
		if (userMapper == null) {
			userMapper = new UserMapper();
		}
		return userMapper;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.core.mapper.Mapper#mapToValue(java.lang.Object)
	 */
	public DTOUser mapToValue(User entity) {
		if(null != entity) {
			DTOUser value = null;
//			if (entity instanceof Supplier){
//				value = new DTOSupplier();
//			}else{
				value = new DTOUser();
//			}
			BeanUtils.copyProperties(entity, value, new String[]{});
//			value=super.mapToValue(entity);
			if(CollectionUtils.isNotEmpty(entity.getAuthoritys())) {
				value.setDtoAuthorities(AuthorityMapper.getInstance().mapToValues(entity.getAuthoritys()));
			}
			if (StringUtil.isEmpty(value.getPassword()))
				value.setPassword("");
//			if(CollectionUtils.isNotEmpty(entity.getAgreements())) {
//				value.setDtoAgreements(AgreementMapper.getInstance().mapToValues(entity.getAgreements()));
//			}
//			value.setTelephone(entity.getTelephone());
			return value;
		}
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.core.mapper.Mapper#mapToValue(java.lang.Object)
	 */
	public List<DTOUser> mapToValues(List<User> entitys) {
		List<DTOUser> dtoUsers=new ArrayList<DTOUser>();
		for(User entity:entitys){
			dtoUsers.add(mapToValue(entity));
		}
		return dtoUsers;
	}
	/*
	 * (non-Javadoc)
	 * @see com.hna.adt.core.mapper.Mapper#mapToEntity(java.lang.Object)
	 */
	public User mapToEntity(DTOUser value) {
		if (null==value)
			return null;
		
		User entity = null;

		try {
			entity = getEntityClass().newInstance();
		} catch (InstantiationException e) {
			throw new RuntimeException("Error creating new instance of : " + getEntityClass().getName(), e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException("Error creating new instance of : " + getEntityClass().getName(), e);
		}

		BeanUtils.copyProperties(value, entity, new String[]{"id","authorities", "agreements"});
		if (value != null && value.getId() != null) {
			entity.setId(value.getId());
		}
		if(CollectionUtils.isNotEmpty(value.getDtoAuthorities())) {
			entity.getAuthoritys().addAll(AuthorityMapper.getInstance().mapToEntities(value.getDtoAuthorities()));
		}
//		if(CollectionUtils.isNotEmpty(value.getDtoAgreements())) {
//			List<Agreement> list = AgreementMapper.getInstance().mapToEntities(value.getDtoAgreements());
//			entity.getAgreements().addAll(list);
//		}
		entity.setTelephone(value.getTelephone());
		return entity;
	}
}
